/*
*******************************************************************************
**
**  This device driver was created by Applilet2 for V850ES/Fx3
**  32-Bit Single-Chip Microcontrollers
**
**  Copyright(C) 2002, 2013 Renesas Electronics Corporation
**  All rights reserved by Renesas Electronics Corporation.
**
**  This program should be used on your own responsibility.
**  Renesas Electronics Corporation assumes no responsibility for any losses
**  incurred by customers or third parties arising from the use of this file.
**
**  Filename :	serial.c
**  Abstract :	This file implements device driver for Serial module.
**  APIlib :	Applilet2 for V850ES/Fx3 V2.43 [30 Mar 2011]
**
**  Device :	uPD70F3376
**
**  Compiler :	CA850
**
**  Creation date:	2013-05-09
**  
*******************************************************************************
*/

/*
*******************************************************************************
**  Include files
*******************************************************************************
*/
#include "macrodriver.h"
#include "serial.h"
/* Start user code for include definition. Do not edit comment generated here */
/* End user code for include definition. Do not edit comment generated here */
#include "user_define.h"
#include "spi_if.h"

/*
*******************************************************************************
**  Global define
*******************************************************************************
*/
volatile UCHAR*	gpCsib1RxAddress;	/* CSIB1 receive buffer address */
volatile USHORT	gCsib1RxLen;	/* CSIB1 receive data length */
volatile USHORT	gCsib1RxCnt;	/* CSIB1 receive data count */
volatile UCHAR*	gpCsib1TxAddress;	/* CSIB1 send buffer address */
volatile USHORT	gCsib1TxLen;	/* CSIB1 send data length */
volatile USHORT	gCsib1TxCnt;	/* CSIB1 send data count */
/* Start user code for global definition. Do not edit comment generated here */
/* End user code for global definition. Do not edit comment generated here */


/*
**-----------------------------------------------------------------------------
**
**	Abstract:
**		This function initializes CSIB1.
**
**	Parameters:
**		None
**
**	Returns:
**		None
**
**-----------------------------------------------------------------------------
*/


void CSIB1_Init(void)
{

/* CBnCTL0
** ----------------------------------------------------------
**     7  |   6   |    5  |     4  | 3 |  2 |   1    |  0
** CBnPWR  CBnTXE  CBnRXE   CBnDIR   0    0   CBnTMS   CBnSCE
**
**
** CBnCTL1
** -------------------------------------------------------------------
**     7  |   6   |    5  |     4  |    3   |     2   |     1    |    0
**     0      0        0    CBnCKP   CBnDAP   CBnCKS2    CBnCKS10  CBnCKS0 
**
** CBnCTL1
** -------------------------------------------------------------------
**     7  |   6   |    5  |     4  |    3   |     2   |     1    |     0
**     0      0        0        0    CBnCL3   CBnCL2      CBnCL1    CBnCL0
*/


	CB1PWR = 0;	/* CSIB1 disable */
	CB1TMK = 1;	/* INTCB1T disable */
	CB1TIF = 0;	/* INTCB1T IF clear */
	CB1RMK = 1;	/* INTCB1R disable */
	CB1RIF = 0;	/* INTCB1R IF clear */
	/* Set INTCB1R lowest priority */
	CB1RIC |= 0x07;
/*	CB1CTL1 = CSIB_DATA_TIMING1 | CSIB_CLOCK_2;  */
	CB1CTL1 = CSIB_DATA_TIMING3 | CSIB_CLOCK_6;  
	CB1CTL2 = CSIB_DATA_LENGTH_8;
//	CB1CTL0 = CSIB_TRANSMIT_ENABLE | CSIB_RECEIVE_ENABLE | CSIB_DATA_MSB | CSIB_MODE_SINGLE | CSIB_STARTTRG_VALID;
	CB1CTL0 = CSIB_TRANSMIT_ENABLE | CSIB_RECEIVE_ENABLE | CSIB_DATA_MSB | CSIB_MODE_CONTINUOUS | CSIB_STARTTRG_VALID;	
	
	/* SCKB1 pin set */
	PFC9H |= 0x02;        // 0000 0010 
	PFCE9H &= 0xFD;       // 1111 1101
	PMC9H |= 0x02;        // 0000 0010
	/* SOB1 pin set */
	PFC9H |= 0x01;        // 0000 0001
	PFCE9H &= 0xFE;       // 1111 1110
	PMC9H |= 0x01;        // 0000 0001
	/* SIB1 pin set */
	PFC9L |= 0x80;        // 1000 0000
	PFCE9L &= 0x7F;       // 0111 1111
	PMC9L |= 0x80;        // 1000 0000
	
}

/*
**-----------------------------------------------------------------------------
**
**	Abstract:
**		This function starts CSIB1 transfer.
**
**	Parameters:
**		None
**
**	Returns:
**		None
**
**-----------------------------------------------------------------------------
*/
void CSIB1_Start(void)
{
	CB1RIF = 0;	/* INTCB1R IF clear */
	CB1RMK = 0;	/* INTCB1R enable */
	CB1PWR = 1;	/* CSIB1 enable */
}

/*
**-----------------------------------------------------------------------------
**
**	Abstract:
**		This function stops CSIB1 transfer.
**
**	Parameters:
**		None
**
**	Returns:
**		None
**
**-----------------------------------------------------------------------------
*/
void CSIB1_Stop(void)
{
	CB1PWR = 0;	/* CSIB1 disable */
	CB1RMK = 1;	/* INTCB1R disable */
	CB1RIF = 0;	/* INTCB1R IF clear */
}

/*
**-----------------------------------------------------------------------------
**
**	Abstract:
**		This function sends and receives CSIB1 data .
**
**	Parameters:
**		txbuf :	transfer buffer pointer
**		txnum :	buffer size
**		rxbuf :	receive buffer pointer
**
**	Returns:
**		MD_OK
**		MD_ARGERROR
**
**-----------------------------------------------------------------------------
*/
MD_STATUS CSIB1_SendReceiveData(UCHAR* txbuf, USHORT txnum, UCHAR* rxbuf)
{
	MD_STATUS status = MD_OK;
	
	if (txnum < 1)
	{
		status = MD_ARGERROR;
	}
	else
	{
		gCsib1TxLen = txnum;	/* send data length */
		gCsib1TxCnt = txnum;	/* send data count */
		gpCsib1TxAddress = txbuf;	/* send buffer pointer */
		gpCsib1RxAddress = rxbuf;	/* receive buffer pointer */
		gCsib1RxCnt = 0;
		if (CB1CTL2 !=  CSIB_DATA_LENGTH_8)
		{
			/* Transfer data length is more than 8 bit */
			/* The data number should be even */
			if (txnum & 0x1){
				status = MD_ARGERROR;
			}
			else
			{
				CB1RMK = 1;	/* INTCB1R disable */
				CB1TX = *(USHORT* )gpCsib1TxAddress;	/* start by writing data to CB1TX */
				gpCsib1TxAddress += 2;
				gCsib1TxCnt -= 2;
				CB1RMK = 0;	/* INTCB1R enable */
				status = MD_OK;
			}
		}
		else
		{
			/* Transfer data length is 8 bit */
			CB1RMK = 1;	/* INTCB1R disable */
			CB1TXL = *gpCsib1TxAddress;	/* start by writing data to CB1TX */
			gpCsib1TxAddress++;
			gCsib1TxCnt--;
			CB1RMK = 0;	/* INTCB1R enable */
			status = MD_OK;
		}
	}
	
	return (status);
}

/* Start adding user code. Do not edit comment generated here */
/* End user code adding. Do not edit comment generated here */


